﻿using System;

namespace LocalOverride.Fiddler.Store
{
    [Serializable]
    public class MappingEntry
    {
        public string Url { get; set; }
        public string LocalPath { get; set; }
        public bool IsDirectory { get; set; }

        public FileExtensionList FileExtensions { get; set; }
        public SpecialCaseMappingList SpecialCaseMappings { get; set; }
        public bool DownloadLocalCopy { get; set; }

        public string DefaultFileName { get; set; }

        public MappingEntry()
        {
            FileExtensions = new FileExtensionList();
            SpecialCaseMappings = new SpecialCaseMappingList();
        }

        public bool Equals(MappingEntry other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.Url, Url) && Equals(other.LocalPath, LocalPath) && other.IsDirectory.Equals(IsDirectory) && Equals(other.FileExtensions, FileExtensions) && Equals(other.SpecialCaseMappings, SpecialCaseMappings) && other.DownloadLocalCopy.Equals(DownloadLocalCopy) && Equals(other.DefaultFileName, DefaultFileName);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (MappingEntry)) return false;
            return Equals((MappingEntry) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = (Url != null ? Url.GetHashCode() : 0);
                result = (result*397) ^ (LocalPath != null ? LocalPath.GetHashCode() : 0);
                result = (result*397) ^ IsDirectory.GetHashCode();
                result = (result*397) ^ (FileExtensions != null ? FileExtensions.GetHashCode() : 0);
                result = (result*397) ^ (SpecialCaseMappings != null ? SpecialCaseMappings.GetHashCode() : 0);
                result = (result*397) ^ DownloadLocalCopy.GetHashCode();
                result = (result*397) ^ (DefaultFileName != null ? DefaultFileName.GetHashCode() : 0);
                return result;
            }
        }

        private string mementoUrl;
        private bool mementoIsDirectory;
        private string mementoLocalPath;
        private bool mementoDownloadLocalCopy;
        private FileExtensionList mementoFileExtensions;
        private string mementoDefaultFileName;

        public virtual void BeginChange()
        {
            mementoUrl = Url;
            mementoIsDirectory = IsDirectory;
            mementoLocalPath = LocalPath;
            mementoDownloadLocalCopy = DownloadLocalCopy;
            mementoFileExtensions = FileExtensions;
            mementoDefaultFileName = DefaultFileName;
        }

        public virtual void CancelChange()
        {
            Url = mementoUrl;
            IsDirectory = mementoIsDirectory;
            LocalPath = mementoLocalPath;
            DownloadLocalCopy = mementoDownloadLocalCopy;
            FileExtensions = mementoFileExtensions;
            DefaultFileName = mementoDefaultFileName;
        }
    }
}